//对对象进行过滤
function filterObjectField(resource, propertyArr, pattern) {
  if(pattern === 'save') { //保留字段
    const cpoyObj = {}
    cpoyObj.__proto__  = resource.__proto__ //保存作用域链
    for(let property of propertyArr) {
      if(property in resource) cpoyObj[property] = resource[property]
    }
    return cpoyObj
  }else if(pattern === 'remove') {
    for(let property of propertyArr) {
      resource[property] = undefined
    }
    return resource
  }
}

const changeObjectField = ((function(){
  //改变对象的字段，数据不变
  function changeField(resource, oldField, newField) {
    const value = resource[oldField]
    resource[newField] = value
    resource[oldField] = undefined
  }
  //处理参数
  function changeObjectField(resource, fieldMap) {
    if(typeof fieldMap != 'object') return //不是对象
    const fieldMapType = Array.isArray(fieldMap)
    if(fieldMapType) { //数组
      const oldField = fieldMap[0]
      const newField = fieldMap[1]
      changeField(resource, oldField, newField)
    }else { //对象
      for(let pro in fieldMap) {
        const oldField = pro
        const newField = fieldMap[pro]
        changeField(resource, oldField, newField)
      }
    }
    return resource
  }
  return changeObjectField
})())

// function getAttrsArr(topMoel, nameArr = []) {
//   let AttributesArr = []
//   AttributesArr.push(getModelAttrs(topMoel))
//   for(let name of nameArr) {
//     topMoel = topMoel[name + '_model']
//     AttributesArr.push(getModelAttrs(topMoel))
//   }
//   return AttributesArr
// }

//求第一个数组中，不存在于第二个数组中
function filterArr(firstArr, secondArr) {
  return firstArr.filter((value)=>{
    let targetindex = secondArr.indexOf(value)
    if(targetindex === -1) {
      return value
    }
  })
}

//model的字段
function getModelAttrs(model) {
  if(model && model.__proto__ && model.__proto__.rawAttributes) {
    return Object.keys(model.__proto__.rawAttributes)
  }else {
    return []
  }
}

//model原型的字段
function getModelClassAttrs(ModelClass) {
  if(ModelClass && ModelClass.prototype) {
    return Object.keys(ModelClass.prototype.rawAttributes)
  }else {
    return []
  }
}


module.exports = {
  filterObjectField,
  changeObjectField,
  getModelAttrs,
  getModelClassAttrs,
  filterArr
}